#pragma once

#include "UMLEdge.h"

#include "../dc/Point.h"
#include "UMLDiagramElement.h"

namespace uml {

UMLEdge::UMLEdge(const QString& umlType)
    : UMLDiagramElement(umlType) {}

void UMLEdge::fromJson(const QJsonObject& json, QStringList& errorList) {
    if (json.isEmpty()) {
        return;
    }

    UMLDiagramElement::fromJson(json, errorList);
    source_ = JsonSerializable::create<UMLDiagramElement>(json.value("source"), errorList);

    target_ = JsonSerializable::create<UMLDiagramElement>(json.value("target"), errorList);

    JsonSerializable::fromJsonArray(waypoints_, "waypoints", json, errorList);
}

void UMLEdge::toJson(QJsonObject& json) {
    UMLDiagramElement::toJson(json);
    if (source_) {
        QJsonObject source_json;
        source_->toJson(source_json);
        json.insert("source", source_json);
    }
    if (target_) {
        QJsonObject target_json;
        target_->toJson(target_json);
        json.insert("target", target_json);
    }
    JsonSerializable::toJsonArray(waypoints_, "waypoints", json);
}

/// <summary>
/// source getter
/// Restricts the sources of UMLEdges to UMLDiagramElements.
/// </summary>
shared_ptr<UMLDiagramElement> UMLEdge::source() const {
    return source_;
}

/// <summary>
/// source setter
/// Restricts the sources of UMLEdges to UMLDiagramElements.
/// </summary>
void UMLEdge::setSource(shared_ptr<UMLDiagramElement> value) {
    source_ = value;
}

/// <summary>
/// target getter
/// Restricts the targets of UMLEdges to UMLDiagramElements.
/// </summary>
shared_ptr<UMLDiagramElement> UMLEdge::target() const {
    return target_;
}

/// <summary>
/// target setter
/// Restricts the targets of UMLEdges to UMLDiagramElements.
/// </summary>
void UMLEdge::setTarget(shared_ptr<UMLDiagramElement> value) {
    target_ = value;
}

/// <summary>
/// waypoints getter
/// an optional list of points relative to the origin of the nesting diagram that
/// specifies the connected line segments of the edge
/// </summary>
vector<shared_ptr<Point>>& UMLEdge::waypoints() {
    return waypoints_;
}

} // namespace uml
